using System;
using Anvil.Core;

namespace Anvil.Core.Commands
{
	/// <summary>
	/// Confirm that another entry does not match a query.
	/// </summary>
	/// <remarks><p>
	/// The underlying query should test whatever fields apply, 
	/// and set as "key" the field name being tested
	///  (e.g. "case_number"). 
	///  If the query matches another record, 
	///  the command raises the default alert message for the field.
	/// </p></remarks>
	public class BaseUnique : BaseList
	{
		public override bool RequestExecute(IRequestContext context)
		{
			PreProcess(context);
			object result = Mapper.QueryForObject(QueryID, context);
			int count = Convert.ToInt32(result);
			bool duplicate = count > 0;
			if (duplicate)
			{
				string key = context[Tokens.KEY] as string;
				context.AddAlertForField(key);
			}

			return CONTINUE;
		}
	}
}